package com.ibm.ops.parser;

import java.util.List;

import org.apache.log4j.Logger;

import com.ibm.ops.domain.metadata.OpsAuditLogReportMetaData;
import com.ibm.ops.domain.report.OpsAuditLogReport;
import com.ibm.ops.exception.CreateReportException;
import com.ibm.ops.exception.PersistentIOException;
import com.ibm.ops.exception.RetrieveReportDataException;
import com.ibm.ops.persistent.PersistentHandler;

abstract class ParserTemplate extends Parser {

	private static Logger logger = Logger.getLogger(ParserTemplate.class);
	
	public ParserTemplate(PersistentHandler persistentHanlder) {
		super(persistentHanlder);
	}

	protected OpsAuditLogReport report = null;
	
	@Override
	public OpsAuditLogReport parse(String content) throws CreateReportException,RetrieveReportDataException, PersistentIOException{
		logger.debug("parse the audit log " + this);
		this.beforeParseReportData(content);
//		this.report = this.createReport();
		List<String> reportUsefulData = this.retrieveReportData(content);
		if(this.report != null){
			int count = reportUsefulData.size();
			for(int i = 0; i < count ; i++){				
				this.report.addReportData(reportUsefulData.get(i));
			}
			
			List<OpsAuditLogReportMetaData> groupedList = this.report.constructReportFormat();
			if(this.persistentHanlder != null){
				this.persistentHanlder.persistent(groupedList);
			}
		}				
		this.afterParseReportData(content);
		logger.debug("end parse the log " + this);
		return this.report;
	}

//	protected abstract OpsAuditLogReport createReport() throws CreateReportException;
	
	protected abstract List<String> retrieveReportData(String rawContent) throws RetrieveReportDataException;
	
	/**
	 * Hook method
	 * @param content
	 */
	protected void beforeParseReportData(String content){}
	
	/**
	 * Hook method
	 * @param content
	 */
	protected void afterParseReportData(String content){
		//this.report.constructReportFormat();
	}
	
}
